<?php
/**
 * @author sunny
 * @date 2023/7/27
 * @time 9:23 上午
 * @focus love & life
 */
namespace app\api\controller;
use think\response\Json;
use hg\apidoc\annotation as Apidoc;
/**
 * 匹配站点
 *
 * SiteMatch
 *
 * @Apidoc\Title("匹配站点")
 * @Apidoc\Group("api")
 */
class SiteMatch extends \app\BaseController
{
    private $result_site;//最终匹配到的站点site_demo_id 
    private $result_https;//客户站点是https还是http 
    /**
     * checkWeburl
     * @Apidoc\Title("根据domain来匹配是哪个系统开发的")
     */
    public function checkWeburl(){
        $domain=input("domain","","trim");
        if(!$domain){
            return error("请输入域名");
        }
        $pattern = '/^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,}$/i';
        if(!preg_match($pattern, $domain)){
            return error("请输入正确的域名");
        }
        if($this->getMatch($domain)){
            $keywordsurl=($this->result_https?"https://":"http://").$domain;
            $keywords=$this->getKeywords($keywordsurl);
            return success("匹配成功",["site_id"=>$this->result_site['id'],"https"=>$this->result_https,"keywords"=>$keywords["keywords"],"title"=>$keywords["title"]]);
        }else{
            $member_id=input("member_id",0,"intval");
            \app\common\model\SiteRecord::create([
                "member_id"=>$member_id,
                "website"=>$domain,
            ]);
            return error("未匹配到","notmatch");
        }
    }
    //match
    private function getMatch($domain){
        $site_demo= \app\common\model\SiteDemo::select();
        foreach($site_demo as $site){
            if($site["system_identification_url"]){
                $url="https://".$domain."/".$site["system_identification_url"];
                $httpCode=$this->curl($url);
                if($httpCode==200){
                    $this->result_site=$site;
                    $this->result_https=1;
                    return true;
                }else{
                    $url="http://".$domain."/".$site["system_identification_url"];
                    $httpCode=$this->curl($url);
                    if($httpCode==200){
                        $this->result_site=$site;
                        $this->result_https=0;                        
                        return true;
                    }
                }
                //测试用的
                //echo "站点名称:".$site["name"]."==状态码:".$httpCode."==URL:<a href='{$url}' target='_blank'>".$url."</a>.<br>\r\n";
            }
        }
        //exit;
        return false;
    }
    //根据url返回状态码
    private function curl($url){
        $ch = curl_init($url);
        //模拟蜘蛛访问，防止有的站点返回403禁止访问
        $userAgent = 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)';
        curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);                 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // 禁用 SSL 证书验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

        curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        return $httpCode;
    }
    //根据url返回网站关键词
    private function getKeywords($url){
        $ch = curl_init($url);
        //模拟蜘蛛访问，防止有的站点返回403禁止访问
        $userAgent = 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)';
        curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);                 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // 禁用 SSL 证书验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $response = curl_exec($ch);
        curl_close($ch);
        // Extract keywords from the response using a regular expression
        preg_match('/<meta\s+name="keywords"\s+content="([^"]+)"\s*\/?>/i', $response, $keywordsMatch);
        if (isset($keywordsMatch[1])) {
            $keywords = $keywordsMatch[1];
        }

        // Extract title from the response using a regular expression
        preg_match('/<title>([^<]+)<\/title>/i', $response, $titleMatch);
        if (isset($titleMatch[1])) {
            $title = $titleMatch[1];
        }

        return [
            'keywords' => $keywords,
            'title' => $title
        ];
    }    
}
